<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/15
 * Time: 22:01
 */

namespace App\Library\form_builder\src;
use App\Library\form_builder\src\traits\CheckTrait;
use App\Library\form_builder\src\traits\DatePickerTrait;
use App\Library\form_builder\src\traits\HiddenTrait;
use App\Library\form_builder\src\traits\InputTrait;
use App\Library\form_builder\src\traits\RadioTrait;
use App\Library\form_builder\src\traits\SelectTrait;
use App\Library\form_builder\src\traits\TextareaTrait;
use App\Library\form_builder\src\traits\TimePickerTrait;
use App\Library\form_builder\src\traits\TreeTrait;
use App\Library\form_builder\src\components\Validate;
use App\Library\form_builder\src\components\BaseForm;
use App\Library\form_builder\src\view\View;
class Form
{
    use CheckTrait,
        DatePickerTrait,
        HiddenTrait,
        InputTrait,
        RadioTrait,
        SelectTrait,
        TextareaTrait,
        TimePickerTrait,
        TreeTrait;
    //form组件
    protected $components=[];
    //组件字段
    protected $fields=[];
    //加载js文件
    protected $script=[];
    //加载css文件
    protected $css=[];
    //布局文件
    protected $layout='';//__DIR__."/template/default/layouts/main.html";
    //表单标题
    protected $title='';
    //链接
    protected $action='';
    //表单属性
    protected $props=[];
    //get|post
    protected $method='';
    //是否加载固态布局模板
    protected $is_layout=true;
//    //布局模板文件
//    protected $layout=[
//        'header'=>'',
//        'silder'=>'',
//        'footer'=>'',
//        'main'=>''
//    ];

//    protected $config=[
//        'layui'=>[
//            'css'=> '/css',
//            'js'=>'/js',
//            'template'=> __DIR__ . '/src/template/'
//        ]
//        //css目录
//        //js目录
//        //layout目录
//        //静态模板目录
//    ];
    protected $View='';
    /**
     * Form constructor.
     *
     * @param string $action 提交地址
     * @param array  $components 组件
     */
    public function __construct($action = '', array $components, array $props=[])
    {
        $this->View= new View();
        $this->components($components);
        $this->action = $action;
        $this->props=$props;
    }
    //加载依赖
    public function load_depends(){
        return $this;
    }
    /**
     * @param array $components
     * @return $this
     */
    public function components(array $components = [])
    {
        foreach ($components as $component) {
            $this->append($component);
        }

        return $this;
    }
    /**
     * 追加组件
     *
     * @param FormComponentDriver $component
     * @return $this
     */
    public function append(BaseForm $component)
    {
        $field = $component->getfield();
        if (!isset($this->components[$field]))
            $this->fields[] = $field;
        $this->components[$field] = $component;

        return $this;
    }
    //获取对象的类名
    function class_basename($class)
    {
        $class = is_object($class) ? get_class($class) : $class;
        return basename(str_replace('\\', '/', $class));
    }
    //输出模板及对应的依赖js或css
    //从内到外逐步生成
//        表单内容
//        表单标签
//        依赖的CSS
//        依赖的js
//        布局文件
    public  function load(){
        //初始化

        $View=$this->View;
        if(empty($this->components)){
            throw new \Exception('最少创建一个组件');
        }
        //首先生成表单内容
        $this->create_form($View);
        //输出提交按钮
        $this->SumbitButton();
        //输出表单标记
        $View->form($this->action,$this->props);
        //输出布局文件
        if(!empty($this->layout)){
            $this->create_layout($View);
        }

        //出输出内容
        return $View->get_templed();
        //输出尾部及JS
    }
    //生成表单
    public  function create_form($View){
        $components=[];
        //表单组件
        foreach($this->components as $item =>$value){
            if(in_array($item,$this->fields)){
                $component_file=$this->class_basename($value);//获取类名
                $component_data=$value->build();
                $components[]=[
                    'component_file'=>$component_file,
                    'component_data'=>$component_data
                ];
            }
        }

        $View->render($components);
    }
    public  function create_layout($View){
        if(file_exists($this->layout)){
            $layout_html=file_get_contents($this->layout);
            $View->layout($layout_html);
        }else{
            throw new \Exception('找不到布局文件');
        }
    }
    //加载提交按钮组件
    public function SumbitButton(){
        $this->View->submit_button();
    }
    //创建组件
    public static function create($action,$components){
        return new self($action,$components);
    }
    //设置表单是post还是get
    public  function setMethod($method){
        $this->method=$method;
        return $this;
    }
    //设置表单跳转
    public  function setAction($action){
        $this->action=$action;
        return $this;
    }
    //设置布局
    public  function setLayout($layout){
        $this->layout=$layout;
        return $this;
    }
}